import React, { Component } from 'react';
import {connect} from 'react-redux'
import { create_add_count,create_sub_count } from '../../../store/actions';

class Cate extends Component {
    
    // handleAdd(){
    //     //触发action
    //     this.props.add();
    // }
    // handleSub(){
    //     //触发action
    //     this.props.sub();
    // }
    render() {
        
        var {c,add,sub} = this.props;

        return (
            <div className='cate'>
                cate
               {/*  <button onClick={this.handleAdd.bind(this)}>加1</button>
                <button onClick={this.handleSub.bind(this)}>减1</button> */}
                <button onClick={add}>加1</button>
                <button onClick={sub}>减1</button>
                <span>{ c }</span>
            </div>
        );
    }
}

//connect高阶组件, 负责将store中state数据, 方法以属性的形式写入到组件props中
export default connect(
    (state)=>{ //负责将store中state数据以属性的形式写入到组件props中
        return {
            c: state.count,
            ld: state.loginData
        }
    },
    (dispatch)=>{ //负责将方法以属性的形式写入到组件props中
        return { 
            add(){
                dispatch( create_add_count(1) );
            },
            sub(){
                dispatch( create_sub_count(1) );
            },
        }
    }
)(Cate);